package com.xtoad.ecms.common.utils.generate;

import java.text.MessageFormat;
import java.util.Map;

/**
 * DTO生成类
 *
 * @author ${作者}
 * @date 2021/1/19
 */
public class DtoGenerator implements IGenerator {

    /**
     * 根据包路径和类名生成类
     *
     * @param packagePath 包路径
     * @param modelName   类名
     * @return 生成的类
     */
    @Override
    public String generate(String packagePath, String modelName) {
        System.out.println("generate Dto start ");
        Map<String, String> classComment = Generator.getClassComment(Generator.modelPackage + "." + modelName);
        Map<String, String> classFieldType = Generator.getClassFieldType(Generator.modelPackage + "." + modelName);
        StringBuilder sb = new StringBuilder();
        sb.append("/*\r\n")
                .append(" * Copyright  2019-2029  xtoad\r\n")
                .append(" */\r\n")
                .append("package ").append(packagePath).append(";\r\n")
                .append("\r\n")
                .append("import com.fasterxml.jackson.annotation.JsonFormat;\r\n")
                .append("import com.xtoad.ecms.common.web.base.BaseDTO;\r\n")
                .append("\r\n")
                .append("import javax.validation.constraints.NotNull;\r\n")
                .append("import java.util.Date;\r\n")
                .append("\r\n")
                .append("/**\r\n")
                .append(" * ").append(classComment.get(modelName)).append("通用DTO类\r\n")
                .append(" *\r\n")
                .append(" * @author ").append(Generator.author).append("\r\n")
                .append(" * @date ").append(Generator.dateString).append("\r\n")
                .append(" */\r\n")
                .append("public class {0}DTO extends BaseDTO '{'\r\n")
                .append("\r\n");
        for (String field : classComment.keySet()) {
            if (field.equals(modelName)) {
                continue;
            }
            String s = Generator.firstCharUpper(field);
            sb.append("    /**\r\n")
                    .append("     * ").append(classComment.get(field)).append("\r\n")
                    .append("     */\r\n")
                    .append("    private ").append(classFieldType.get(field)).append(" ").append(field).append(";\r\n")
                    .append("\r\n");
        }
        for (String field : classComment.keySet()) {
            if (field.equals(modelName)) {
                continue;
            }
            String s = Generator.firstCharUpper(field);

            sb.append("    /**\r\n")
                    .append("     * 获取 ").append(classComment.get(field)).append("\r\n")
                    .append("     */\r\n")
                    .append("    public ").append(classFieldType.get(field)).append(" get").append(s).append("() '{'\r\n")
                    .append("        return this.").append(field).append(";\r\n")
                    .append("    '}'\r\n")
                    .append("\r\n")
                    .append("    /**\r\n")
                    .append("     * 设置 ").append(classComment.get(field)).append("\r\n")
                    .append("     */\r\n")
                    .append("    public void set").append(s).append("(").append(classFieldType.get(field)).append(" ").append(field).append(") '{'\r\n")
                    .append("        this.").append(field).append(" = ").append(field).append(";\r\n")
                    .append("    '}'\r\n")
                    .append("\r\n");
        }
        sb.append("'}'\r\n");
        System.out.println("generate Dto end ");
        System.out.println(sb.toString());
        return MessageFormat.format(sb.toString(), modelName);
    }
}
